/**
 * 
 */
define(function(){
	//http://127.0.0.1:8080/JCTILoggerAgentConnector/notifystate.jsp?dn=8001&agentid=5001&agentstate=3&workmode=3&reasoncode=1
	var m_ConnectorServer;
	//var m_ConnectorServer1;
	
	var Extra = function(project){
		this.project = project;
		this.name = 'ctiextra_agentstatesender';
		m_ConnectorServer = $SC.getConfig('AgentStateConnector', 'ConnectorServer');
		$SC.addEvtListener(this.name, 'OnOrigCTIEvent', OnCTIEvent, -1);
	}
	Extra.prototype = {		
		queryCTICall : function(){
			
		}
	}

	function OnCTIEvent(evt){
		var e = evt.event.value.toString().toLowerCase();
		switch(e){
		case '70':
		case '72':
		case '73':
			//debugger;
			
			if (!m_ConnectorServer) {
				m_ConnectServer = $SC.getConfig('AgentStateConnector', 'ConnectorServer');
				if (!m_ConnectorServer) {
					m_ConnectorServer = '*'
				}
			}
			if (m_ConnectorServer == '*') {
				return;
			}
			if (e != '72' && e != '73') {
				break;
			}
			
			//debugger;
			var dn = $SC.extension;
			var agentid = $SC.agentId;
			var agentstate = 0;
			var workmode = 0;
			var reasoncode = 0;

			//dn = '5005';
			//agentid = '8005';
			if (e == '72') {
				agentstate = 2;
			} else if (e == '73') {
				agentstate = 3;
			}
			if (evt.workmode) {
				workmode = evt.workmode.value;
			}
			if (evt.reasoncode) {
				reasoncode = evt.reasoncode.value;
		        if (isNaN(reasoncode) || Number(reasoncode) < 0) {
		        	reasoncode = "0";
		        } else {
			        reasoncode = Number(reasoncode);
				}
			}

		    var xmlHttp = new XMLHttpRequest();
		    var m_url = m_ConnectorServer;
		    m_url = m_url.replace(/\[dn\]/gi, dn);
		    m_url = m_url.replace(/\[agentid\]/gi, agentid);
		    m_url = m_url.replace(/\[agentstate\]/gi, agentstate);
		    m_url = m_url.replace(/\[workmode\]/gi, workmode);
		    m_url = m_url.replace(/\[reasoncode\]/gi, reasoncode);
		    m_url = m_url.replace(/{dn}/gi, dn);
		    m_url = m_url.replace(/{agentid}/gi, agentid);
		    m_url = m_url.replace(/{agentstate}/gi, agentstate);
		    m_url = m_url.replace(/{workmode}/gi, workmode);
		    m_url = m_url.replace(/{reasoncode}/gi, reasoncode);
		    xmlHttp.open("GET", m_url);
		    xmlHttp.send();
		    xmlHttp.onload = function () {
		    	debugger;
		    	if (this.status == 200 ) {
		    		//this.response;
		    	} else {
		    		//this.statusText);
		    	}
		    };
		    xmlHttp.onerror = function () {
		    	debugger;
		    	//this.statusText);
		    };
		}
	}


	return Extra; 
});

